package com.intel.factorytest.activity;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.intel.factorytest.R;
public class GyroTest extends Activity implements SensorEventListener {
    private GLSurfaceView intel_mat_mGLSurfaceView;
    private SensorManager intel_mat_mSensorManager;
    private float[] intel_mat_mGData = new float[3];
    private float[] intel_mat_mMData = new float[3];
    private float[] intel_mat_mR = new float[16];
    private float[] intel_mat_mI = new float[16];
    private FloatBuffer intel_mat_mVertexBuffer;
    private FloatBuffer intel_mat_mColorBuffer;
    private ByteBuffer intel_mat_mIndexBuffer;
    private float[] intel_mat_mOrientation = new float[3];
    private int intel_mat_mCount;
    private int intel_mat_OrientationEnable;

    private TextView intel_mat_mInfo ;
    private String intel_mat_mPitchStr="";
    private String intel_mat_mRoll="";
    private String intel_mat_mYaw="";
    private String intel_mat_mInfoStr="";
    private float intel_mat_gx1 = 0;
    private float intel_mat_gy1 = 1;
    private float intel_mat_gz1 = 2;
    private float intel_mat_gx = 0;
    private float intel_mat_gy = 1;
    private float intel_mat_gz = 2;
    private int intel_mat_count = 0;
    private String intel_mat_tmp = null;  
    private static final String TEST_ITEM = "test_item";
    private static final String TEST_ITEM_RESULT = "test_item_result";
    private boolean is_pass;
    private static final String TEST_RESULT = "test_result";

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String orientationStr = intent.getStringExtra("sd_orientation");
    int orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    Log.i("util","orientationStr = " + orientationStr);
    // hugh add start
    if (orientationStr == null || orientationStr.equals("")) {
        orientationStr = "SCREEN_ORIENTATION_PORTRAIT";
    }
    // hugh add end
    if(orientationStr.equals("SCREEN_ORIENTATION_PORTRAIT")) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        super.setRequestedOrientation(orientation);

    } else if(orientationStr.equals("SCREEN_ORIENTATION_LANDSCAPE")) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        super.setRequestedOrientation(orientation);

    }
        intel_mat_mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        intel_mat_mInfo = new TextView(this);
    intel_mat_mInfo.setTextSize(24.0f);
        intel_mat_mInfo.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        intel_mat_mInfo.setGravity(Gravity.CENTER);
        setContentView(intel_mat_mInfo);
    }

    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        intel_mat_mPitchStr = getResources().getString(R.string.SERVICEMENU_GYRO_PITCH_TXT);
        intel_mat_mRoll = getResources().getString(R.string.SERVICEMENU_GYRO_ROLL_TXT);
        intel_mat_mYaw = getResources().getString(R.string.SERVICEMENU_GYRO_YAW_TXT);
        intel_mat_mInfoStr = getResources().getString(R.string.SERVICEMENU_GYRO_HELP_INFO);
        Sensor msensor = intel_mat_mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    if(msensor == null) {
        intel_mat_mInfo.setText(R.string.SERVICEMENU_GYRO_UNAVAILABLE_TXT);
    } else {
            intel_mat_mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
    }
    }

    @Override
    protected void onPause() {
        super.onPause();
        intel_mat_mSensorManager.unregisterListener(this);
    } 

   
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        int type = event.sensor.getType();
    
        if (type == Sensor.TYPE_GYROSCOPE) {
        if (intel_mat_count == 0) {
            intel_mat_gx = event.values[0];
            intel_mat_gy = event.values[1];
            intel_mat_gz = event.values[2];
            intel_mat_tmp = intel_mat_mPitchStr+intel_mat_gx+"\n"
                           +intel_mat_mRoll+intel_mat_gy+"\n"
                           +intel_mat_mYaw+intel_mat_gz+"\n"
                           +intel_mat_mInfoStr;
        }else {
            intel_mat_gx1 = event.values[0];
            intel_mat_gy1 = event.values[1];
            intel_mat_gz1 = event.values[2];
            intel_mat_tmp = intel_mat_mPitchStr+intel_mat_gx1+"\n"
                           +intel_mat_mRoll+intel_mat_gy1+"\n"
                           +intel_mat_mYaw+intel_mat_gz1+"\n"
                           +intel_mat_mInfoStr;
        }
        intel_mat_count++;
        if(intel_mat_count >= 40) {
            if (intel_mat_gx!=intel_mat_gx1 || intel_mat_gy!=intel_mat_gy1 || intel_mat_gz!=intel_mat_gz1) {
                intel_mat_tmp = intel_mat_tmp +"\n"+getString(R.string.SERVICEMENU_GYRO_OK_TXT);
                is_pass = true;
            }else {
                is_pass = false;
            }
        }
        intel_mat_mInfo.setText(intel_mat_tmp);
        }
    }

    
    @Override
    protected Dialog onCreateDialog(int none)
    {        
        return new AlertDialog.Builder(this)
//        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
        .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked OK so do some stuff */
                finish();
            }
        })
        .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked Cancel so do some stuff */
                finish();
            }
        })
        .create();
    }
    
    private void setTestResult () {
        Intent intent = new Intent();
        Intent mIntent = getIntent();
        int pos = mIntent.getIntExtra(TEST_ITEM,-1);
        intent.putExtra(TEST_ITEM,pos);
        intent.putExtra(TEST_ITEM_RESULT,is_pass);
//        Log.e(TAG,"pos ="+pos);
        setResult(0, intent);
    }
    @Override
    public void finish() {
        setTestResult (); 
        super.finish();
    }
    public boolean ac_ispass() {
        return is_pass;
    } 
}
